#include "../inc/Ui/TwitterReplyForm.h"
#include "../inc/Model/TwitterMySnsGatewayManager.h"
#include "../inc/Ui/TwitterForm.h"
#include"../inc/Ui/FormManager.h"
using namespace Osp::Base;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;
int TwitterReplyForm::FormId = -1;

TwitterReplyForm::TwitterReplyForm(void) {
}

TwitterReplyForm::~TwitterReplyForm(void) {
}

bool TwitterReplyForm::Initialize() {
	Form::Construct(L"Twitter_ReplyForm");

	return true;
}

result TwitterReplyForm::OnInitializing(void) {
	result r = E_SUCCESS;

	// TODO: Add your initialization code here

	Button *pButton_reply = static_cast<Button *> (GetControl(
			"IDC_BUTTON_REPLY"));
	if (pButton_reply) {
		pButton_reply->SetActionId(ID_BUTTON_REPLY);
		pButton_reply->AddActionEventListener(*this);
	}
	pEditFieldReply
			= static_cast<EditField*> (GetControl("IDC_EDITFIELD_REPLY"));
	pLabelReply = static_cast<Label*> (GetControl("ID_LABLE_REPLY"));
	pLabelNOTICED = static_cast<Label*> (GetControl("IDC_LABEL_NOTICE"));

	this->SetSoftkeyActionId(SOFTKEY_1, ID_SOFTKEY_BACK);
	this->AddSoftkeyActionListener(SOFTKEY_1, *this);

	return r;
}

result TwitterReplyForm::OnTerminating(void) {
	result r = E_SUCCESS;

	// TODO: Add your termination code here

	return r;
}

void TwitterReplyForm::OnActionPerformed(const Osp::Ui::Control &source,
		int actionId) {
	FormManager *pFormManager = null;
	Form *pTwitterForm = null;
	FormManager::GetInstance(pFormManager);

	result r = pFormManager->GetForm(TwitterForm::GetFormId(), pTwitterForm);
	if (IsFailed(r)) {
		return;
	}
	MySnsGatewayManager
			*mySnsGateway =
					static_cast<TwitterForm*> (pTwitterForm)->GetSnsGatewayTwitterForm();

	switch (actionId) {
	case ID_BUTTON_REPLY: {
		String commentStatus = pEditFieldReply->GetText();
		if (commentStatus.GetLength() > 0) {
			pLabelNOTICED->SetText("(*Comment khong duoc rong)");
		}
		if (commentStatus.GetLength() > 140) {
			pLabelNOTICED->SetText("(*Comment khong duoc dai hon 140 ky tu)");
		}
		if (commentStatus.GetLength() > 0 && commentStatus.GetLength() < 140) {
			AppLog("Vao reply");
			mySnsGateway->UpdateMyStatusText(commentStatus);
			pFormManager->SetCurrentForm(MyFollowingForm::GetFormId());
		}
	}
		break;
	case ID_SOFTKEY_BACK: {
		pFormManager->SetCurrentForm(MyFollowingForm::GetFormId());
	}
		break;
	default:
		break;
	}

}
void TwitterReplyForm::UpdateInfomationOfUserName(String userName) {
	String displayLable;
	displayLable.Append("Reply To ");
	displayLable.Append(userName);
	pLabelReply->SetText(displayLable);

	String commentUserName;
	commentUserName.Append("@");
	commentUserName.Append(userName);
	pEditFieldReply->SetText(commentUserName);
	this->RequestRedraw(true);
	pLabelNOTICED->SetText("");
}
